home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_kdelibs.idb / usr / freeware / kde / include / drag.h.z / drag.h
Encoding:
C/C++ Source or Header  |  1999-01-26  |  14.0 KB  |  574 lines

  1. /* This file is part of the KDE libraries
  2.     Copyright (C) 1997 Torben Weis (weis@kde.org)
  3.  
  4.     This library is free software; you can redistribute it and/or
  5.     modify it under the terms of the GNU Library General Public
  6.     License as published by the Free Software Foundation; either
  7.     version 2 of the License, or (at your option) any later version.
  8.  
  9.     This library is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.     Library General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU Library General Public License
  15.     along with this library; see the file COPYING.LIB.  If not, write to
  16.     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17.     Boston, MA 02111-1307, USA.
  18. */
  19. /* $Id: drag.h,v 1.16 1998/06/16 06:03:14 kalle Exp $
  20.  *
  21.  * $Log: drag.h,v $
  22.  * Revision 1.16  1998/06/16 06:03:14  kalle
  23.  * Implemented copy constructors and assignment operators or disabled them
  24.  *
  25.  * Revision 1.15  1998/05/08 20:45:08  kulow
  26.  * took out the #undef I added
  27.  *
  28.  * Revision 1.14  1998/05/08 16:09:59  kulow
  29.  * undef Color and GrayScale after including X11/X.h. This stupid header breaks
  30.  * everything!
  31.  * How about typedef Window unsigned long instead of X11/X.h?
  32.  *
  33.  * Revision 1.13  1998/01/18 14:38:29  kulow
  34.  * reverted the changes, Jacek commited.
  35.  * Only the RCS comments were affected, but to keep them consistent, I
  36.  * thought, it's better to revert them.
  37.  * I checked twice, that only comments are affected ;)
  38.  *
  39.  * Revision 1.11  1997/12/12 14:40:12  denis
  40.  * Reverting to false :-)
  41.  *
  42.  * Revision 1.10  1997/12/12 14:36:22  denis
  43.  * false to FALSE
  44.  *
  45.  * Revision 1.9  1997/10/16 11:14:26  torben
  46.  * Kalle: Copyright headers
  47.  * kdoctoolbar removed
  48.  *
  49.  * Revision 1.8  1997/09/18 12:16:04  kulow
  50.  * corrected some header dependencies. Removed most of them in drag.h and put
  51.  * them in drag.cpp. Now it should compile even under SunOS 4.4.1 ;)
  52.  *
  53.  * Revision 1.7  1997/07/25 19:46:40  kalle
  54.  * SGI changes
  55.  *
  56.  * Revision 1.6  1997/07/18 05:49:15  ssk
  57.  * Taj: All kdecore doc now in javadoc format (hopefully).
  58.  *
  59.  * Revision 1.5  1997/05/08 22:53:15  kalle
  60.  * Kalle:
  61.  * KPixmap gone for good
  62.  * Eliminated static objects from KApplication and HTML-Widget
  63.  *
  64.  * Revision 1.4  1997/05/06 08:50:53  ssk
  65.  * taj: added X.h to files included. please test.
  66.  *
  67.  * Revision 1.3  1997/05/05 09:25:38  ssk
  68.  * Taj: Attempt to fix the include-order problem. Please test this. My
  69.  *      configure program is still not working, so i can't test this myself.
  70.  *
  71.  * Revision 1.2  1997/04/23 21:21:53  kulow
  72.  * added some Q_OBJECTs
  73.  *
  74.  * Revision 1.1.1.1  1997/04/13 14:42:41  cvsuser
  75.  * Source imported
  76.  *
  77.  * Revision 1.1.1.1  1997/04/09 00:28:07  cvsuser
  78.  * Sources imported
  79.  *
  80.  * Revision 1.5  1997/03/10 20:01:31  kalle
  81.  * Merged changes from 0.6.3
  82.  *
  83.  * Revision 1.4  1997/01/15 20:08:26  kalle
  84.  * merged changes from 0.52
  85.  *
  86.  * Revision 1.3  1996/12/07 22:34:54  kalle
  87.  * DOC++ tags corrected
  88.  *
  89.  * Revision 1.2  1996/12/07 22:22:06  kalle
  90.  * autoconf, KApplication merged with my KApplication (and moved to kapp.h)
  91.  * formatting
  92.  *
  93.  *
  94.  * Drag and Drop for KDE
  95.  * Torben Weis <weis@stud.uni-frankfurt.de>
  96.  *
  97.  */
  98.  
  99. #ifndef DRAG_H
  100. #define DRAG_H
  101.  
  102. #ifdef HAVE_CONFIG_H
  103. #include <config.h>
  104. #endif
  105.  
  106. #include <qapp.h> // for the TrueColor problem
  107. #include <qwidget.h>
  108. #include <qpixmap.h>
  109. #include <qstrlist.h>
  110. #include <X11/X.h>
  111.  
  112. /* Usually we drag URLs around. Every type of data must have
  113.   a unique ID so that the receiver can decide wether he wants to
  114.   accept the drop or not.
  115.   */
  116. // ???
  117. #define DndNotDnd       -1
  118. // ???
  119. #define DndUnknown      0
  120. // Raw data
  121. #define DndRawData      1
  122. // Dont use that any more
  123. #define DndFile         2
  124. // Dont use that any more
  125. #define DndFiles        3
  126. // ASCII Text
  127. #define DndText         4
  128. // Dont use that any more
  129. #define DndDir          5
  130. // Dont use that any more
  131. #define DndLink         6
  132. // Dont use that any more
  133. #define DndExe          7
  134. // For Offix internal use only
  135. #define DndEND          8
  136. // An URL
  137. #define DndURL          128
  138.  
  139. #define Dnd_X_Precision 2
  140. #define Dnd_Y_Precision 2
  141.  
  142. #define kapp KApplication::getKApplication()
  143.  
  144. /**
  145. * Icon for KDE Drag 'n Drop. This is the widget that is moved
  146. * around during DND.
  147. *
  148. * @short Icon for KDE Drag 'n Drop
  149. * @author Torben Weis (weis@kde.org)
  150. * @version $Id: drag.h,v 1.16 1998/06/16 06:03:14 kalle Exp $
  151. */
  152. class KDNDIcon : public QWidget
  153. {
  154.   Q_OBJECT
  155. public:
  156.  
  157. /**
  158. * Creates an Icon with the specified pixmap. _x and _y are the upper
  159. * left corner in global coordinates.
  160. */
  161.   KDNDIcon( QPixmap &pixmap , int _x, int _y );
  162.  
  163.   /**
  164.    * Copy constructor 
  165.    */
  166.   KDNDIcon( const KDNDIcon& icon );
  167.   
  168.   /**
  169.    * Assignment operator
  170.    */
  171.   KDNDIcon& operator= ( const KDNDIcon& icon );
  172.  
  173. /** 
  174. * Destructor 
  175. */
  176.   virtual ~KDNDIcon();
  177.     
  178. protected:
  179.   virtual void paintEvent( QPaintEvent * );
  180.   virtual void resizeEvent( QResizeEvent * );
  181.   
  182. /**
  183. * The pixmap displayed.
  184. */
  185.   QPixmap pixmap;
  186. };
  187.  
  188. /**
  189. * Drop zone for KDE Drag n Drop. 
  190. *
  191. * You can create a DropZone for every widget. When the user makes a 
  192. * drop over this widget, the KDNDDropZone takes over control.
  193. *
  194. * The KDE drag and drop classes are based on the Offix Drag and Drop
  195. * protocol, but are not compatible with it yet.
  196. *
  197. * Currently used types are: DndText, DndURL.
  198. *
  199. * @short Drop zone for KDE Drag n Drop.
  200. * @author Torben Weis (weis@kde.org)
  201. * @version $Id: drag.h,v 1.16 1998/06/16 06:03:14 kalle Exp $
  202. */
  203. class KDNDDropZone : public QObject
  204. {
  205.   Q_OBJECT
  206.  
  207. public:
  208. /** 
  209. * Create a DropZone for the widget _parent.
  210. *
  211. * @param _parent    The parent of this dropzone, usually the widget
  212. *            for which you wish to receive drop events.
  213. * @param _type        The type of Drop events to accept, eg DndURL.
  214. */
  215.   KDNDDropZone( QWidget* _parent, int _type );
  216.  
  217. /** 
  218. * Destructor 
  219. */
  220.   virtual ~KDNDDropZone();
  221.  
  222. /** When a drop occurs, this function is called. _data is the
  223.    dropped data, _size its size, _type for example DndURL and
  224.    _x and _y are the global coordinates of the drop.
  225.    */
  226.  
  227. /**
  228. * This function is called when a drop event occurs.
  229. *
  230. * @param _data        A pointer to the dropped data.
  231. * @param _size        The length of the data dropped.
  232. * @param _type        The type of the data, eg DndURL means a URL was
  233. *            dropped.
  234. * @param _x,_y        The global coordinates of the drop.
  235. */
  236.   virtual void drop( char *_data, int _size, int _type, int _x, int _y );
  237.  
  238. /**
  239. * This function is called when an icon is being dragged over this
  240. * drop zone.
  241. *
  242. * Note that the icon may never have left the drop zone; the user may
  243. * be dragging it around withing this zone and this function would still
  244. * be called.
  245. *
  246. * @param _data        A pointer to the dragged data.
  247. * @param _size        The length of the data dragged.
  248. * @param _type        The type of the data, eg DndURL means a URL is
  249. *            being dragged.
  250. * @param _x,_y        The global coordinates of the drag.
  251. */
  252.   virtual void enter( char *_data, int _size, int _type, int _x, int _y );
  253.  
  254. /**
  255. * This function is called when the icon being dragged has left
  256. * this drop zone.
  257. */
  258.   virtual void leave();
  259.  
  260. /**
  261. * Decode the dropped data into a list of URLs. This should only be called
  262. * if the dropped data was of type DndURL.
  263. *
  264. * Note that the contents of this list are only valid till the next
  265. * drop event.
  266. */
  267.   virtual QStrList & getURLList();
  268.     
  269. /** 
  270. * Get dropped data.
  271. *
  272. * @return A pointer to the dropped data.
  273. */
  274.   virtual const char* getData() { return dndData; }
  275.  
  276. /** 
  277. * Get dropped data length.
  278. *
  279. * @return the length of the data that was dropped.
  280. */
  281.   virtual int getDataSize() { return dndSize; }
  282.  
  283. /** 
  284. * Get drop data type.
  285. *
  286. * @return the type of the data dropped.
  287. */
  288.   virtual int getDataType() { return dndType; }
  289.  
  290. /** 
  291. * Get the mouse position at which the item was dropped. 
  292. *
  293. * @return the X coordinate at which the item was dropped.
  294. * @see #getMouseY
  295. */
  296.   virtual int getMouseX() { return dndX; }
  297. /** 
  298. * Get the mouse position at which the item was dropped. 
  299. *
  300. * @return the Y coordinate at which the item was dropped.
  301. * @see #getMouseX
  302. */
  303.   virtual int getMouseY() { return dndY; }
  304.  
  305. /** 
  306. * The types of dropped data this drop zone will accept.
  307. *
  308. * @return the types of drops accepted.
  309. */
  310.   virtual int getAcceptType() { return acceptType; }
  311.  
  312. /**
  313. * Tests whether this data type will be accepted.
  314. *
  315. * @param _type    the data type to be tested.
  316. * @return TRUE if this type will be accepted, FALSE otherwise.
  317. */
  318.   virtual bool accepts( int _type ) 
  319.   { return (( acceptType & _type ) == _type); }
  320.  
  321. /**
  322. * Get the parent widget.
  323. *
  324. * @return the parent widget for which this object is monitoring drops.
  325. */
  326.   QWidget* getWidget();
  327.  
  328. signals:
  329.  
  330. /** 
  331. * Emitted when a drop has occurred.
  332. * The zone into which the drop has occurred is passed as a parameter.
  333. */
  334.   void dropAction( KDNDDropZone* );
  335.  
  336. /** 
  337. * Emitted when an icon is dragged into and inside this drop zone.
  338. */
  339.   void dropEnter( KDNDDropZone* );
  340.  
  341. /** 
  342. * Emitted when an icon is dragged out of this drop zone.
  343. */
  344.   void dropLeave( KDNDDropZone* );
  345.  
  346. protected:
  347. /** 
  348. * Fills 'urlList' with the URLs in 'dndData'.
  349. * Works only if 'dndType' is DndURL.
  350. */
  351.   void parseURLList();
  352.     
  353. /**
  354. * The parent widget being monitored.
  355. */
  356.   QWidget *widget;
  357.   
  358. /**
  359. * Last drop data.
  360. * Only valid during a call to 'dropAction'
  361. */
  362.   char *dndData;
  363. /**
  364. */
  365.   int dndSize;
  366. /**
  367. */
  368.   int dndType;
  369. /**
  370. * Drop/drag X position.
  371. */
  372.   int dndX;
  373. /**
  374. * Drop/drag Y position.
  375. */
  376.   int dndY;
  377. /**
  378. * Data type mask.
  379. */
  380.   int acceptType;
  381.  
  382. /**
  383. * If 'dndType' is URL, then all URLs in 'dndData' are stored here.
  384. */
  385.   QStrList urlList;    
  386. };
  387.  
  388. /** 
  389. * A widget for drag support.
  390. *
  391. * If you require only drop support you dont need this widget, you just need
  392. * KDndDropZone.
  393. *
  394. * @short A widget for drag support.
  395. * @author Torben Weis (weis@kde.org)
  396. * @version $Id: drag.h,v 1.16 1998/06/16 06:03:14 kalle Exp $
  397. */
  398. class KDNDWidget : public QWidget
  399. {
  400.   Q_OBJECT
  401. public:
  402. /** 
  403. * Constructor. 
  404. */
  405.     KDNDWidget( QWidget *parent=0, const char *name=0, WFlags f=0 ) : 
  406.       QWidget( parent, name, f ) 
  407.   { drag = false; dndData = 0L; dndIcon = 0L; }
  408.  
  409. /** 
  410. * Destructor. 
  411. */
  412.   virtual ~KDNDWidget();
  413.  
  414. /** 
  415. * Start a drag.
  416. *
  417. * Call this function when you notice that the user wants to drag something 
  418. * around, usually from a dndMouseMoveEvent.
  419. *
  420. * @param _icon    The icon that the user can drag around. 
  421. * @param _data    A pointer to the data being dragged. A deep copy is
  422. *        made of this data, so you don't need to maintain its
  423. *        value after you call this function.
  424. * @param _size    The length of the data pointed to by _data.
  425. * @param _type    The type of the data that is being dragged, eg DndURL.
  426. * @param _dx,_dy The difference between the icons upper left corner and 
  427. *        the mouse pointer. For example when the user clicks the 
  428. *        mouse over the middle of a pixmap, _dx and _dy would be 
  429. *        ' - pixmap.width() / 2 ' and ' - pixmap.height() / 2 '. 
  430. *        This is just provided for look and feel.
  431. */
  432.     virtual void startDrag( KDNDIcon *_icon, char *_data, int _size, 
  433.                 int _type, int _dx, int _dy );
  434.  
  435. protected:
  436. /**
  437. * Finds the root window belonging to the global point p.
  438. */
  439.   virtual Window /*KDNDWidget::*/findRootWindow( QPoint & p );
  440.   
  441. /** 
  442. * This function MUST be called by your implementation if you overload it.
  443. *
  444. * In nearly all cases, you probably mean to call dndMouseMoveEvent().
  445. *
  446. * @see #dndMouseMoveEvent
  447. */
  448.   virtual void mouseMoveEvent( QMouseEvent * );
  449.  
  450. /** 
  451. * This function MUST be called by your implementation if you overload it.
  452. *
  453. * In nearly all cases, you probably mean to call dndMouseReleaseEvent().
  454. *
  455. * @see #dndMouseReleaseEvent
  456. */
  457.   virtual void mouseReleaseEvent( QMouseEvent * );
  458.   
  459. /**
  460. * A root drop occurred.
  461. *
  462. * At the point (_x,_y) the user dropped the icon. If there is now window 
  463. * below this point, this function is called.  Usually it emits a XEvent, 
  464. * so that every application gets informed about this. This function is 
  465. * only called if the drag started in this widget. 
  466. *
  467. * See KApplication for details on receiving root drop events.
  468. */
  469.   virtual void rootDropEvent( int _x, int _y );
  470.  
  471. /** 
  472. * Perform internal housekeeping after a root drop event.
  473. *
  474. * If you must overload rootDropEvent(...), call this function at the
  475. * end to do some clean up.
  476. */
  477.   virtual void rootDropEvent();
  478.   
  479. /** 
  480. * Called when a drag is ended.
  481. *
  482. * This function is only called if the drag started in this widget.
  483. * Overload it to do your own clean up.
  484. */
  485.   virtual void dragEndEvent() { }
  486.  
  487. /**
  488. * Overload this instead of mouseMoveEvent. 
  489. * Ususally drags are started in
  490. *    this functions. A implementation might look like this:
  491. *
  492. * <pre>
  493. * void KFileView::dndMouseMoveEvent( QMouseEvent * _mouse )
  494. *    {
  495. *    // 'pressed' is set in mousePressedEvent(...)
  496. *    if ( !pressed )
  497. *    return;
  498. *    
  499. *    int x = _mouse->pos().x();
  500. *    int y = _mouse->pos().y();
  501. *
  502. *    if ( abs( x - press_x ) > Dnd_X_Precision || abs( y - press_y ) > Dnd_Y_Precision )
  503. *    {
  504. *        QString data = "Transfer me";
  505. *    QPoint p = mapToGlobal( _mouse->pos() );
  506. *    QPixmap pixmap = typ->getPixmap( filename );
  507. *    int dx = - pixmap.width() / 2;
  508. *    int dy = - pixmap.height() / 2;
  509. *
  510. *    startDrag( new KDNDIcon( pixmap, p.x() + dx, p.y() + dy ), data.data(), data.length(), DndText, dx, dy );
  511. *    }
  512. *    else
  513. *    {
  514. *      Do something different
  515. *    }
  516. * </pre>
  517. *
  518. *    The function is only called if the mouse movement was not part of a
  519. *    drag process.
  520. */
  521.   virtual void dndMouseMoveEvent( QMouseEvent * ) { }    
  522. /** 
  523. * Your mouse release event function.
  524. *
  525. * Usually you will only set 'pressed' ( see dndMouseMoveEvent) to FALSE here.
  526. * The function is only called if the release event had nothing to do with
  527. * DND.
  528. */
  529.   virtual void dndMouseReleaseEvent( QMouseEvent * ) { }
  530.   
  531. /**
  532. * Are we just doing DND ?
  533. */
  534.   bool drag;
  535. /**
  536. * The data that is currently dragged.
  537. */
  538.   char *dndData;
  539. /**
  540. * data size
  541. */
  542.   int dndSize;
  543. /**
  544. * data type
  545. */
  546.   int dndType;
  547. /**
  548. * The offset we got from 'startDrag'
  549. */
  550.   int dndOffsetX;
  551. /**
  552. * The offset we got from 'startDrag'
  553. */
  554.   int dndOffsetY;
  555. /**
  556. * The icon we are moving around
  557. */
  558.   KDNDIcon *dndIcon;
  559. /**
  560. * The last window we entered with the mouse pointer.
  561. */
  562.   Window dndLastWindow;
  563.  
  564. private:
  565.   // Disallow assignment and copy-construction
  566.   KDNDWidget( const KDNDWidget& ) {};
  567.   KDNDWidget& operator= ( const KDNDWidget& ) { return *this; };
  568. };
  569.  
  570. #endif
  571.  
  572.  
  573.